#ifndef _RequestDispatcher_H_
#define _RequestDispatcher_H_
#include <iostream>
#include <map>
#include <string>
#include "RequestDealer.h"
#include "../Utils/JsonUtil.h"
using namespace std;

//class definations
class RequestDispatcher{
public:
	~RequestDispatcher();
	string dispatchRequest(string json);
private:
	RequestDealer *dealer;
	DealerFactory *factory;
	string operation;
	map<string,string> params;
	void parse(string json);
};

//function defination of RequestDispatcher
RequestDispatcher::~RequestDispatcher(){
	if(this->dealer!=nullptr){
		delete dealer;
	}
	if(this->factory!=nullptr){
		delete factory;
	}
}

void RequestDispatcher::parse(string json){
	this->operation=JsonUtil::parse_operation(json);
	this->params=JsonUtil::parse_param(json);
}

string RequestDispatcher::dispatchRequest(string json){
	parse(json);
	string result;
	this->factory=new DealerFactory;
	this->dealer=factory->createRequestDealer(operation);
	if(dealer==nullptr)
		result="input illegal!";
	else
		result=dealer->dealRequest(params);
	delete factory;
	delete dealer;
	return result;
}

#endif